package com.android.project.games;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import com.android.project.framework.gl.Camera2D;
import com.android.project.framework.gl.SpriteBatcher;
import com.android.project.framework.impl.GLGraphics;

public class WorldRenderer {
	
	static final float FRUSTUM_WIDTH = 320;
	static final float FRUSTUM_HEIGHT = 480;
	World world;
	SpriteBatcher batcher;
	GLGraphics glGraphics;
	Camera2D guiCam;



	
	public WorldRenderer( GLGraphics glGraphics,SpriteBatcher batcher, World world){
		this.glGraphics = glGraphics;
		this.batcher =  batcher;
		this.world = world;
		this.guiCam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
		
		
	}
	
	public void render(String s) {
		// TODO Auto-generated method stub
		
	
		//Log.d("Render", " " + world2.render);
		if(world.render == 0){
			guiCam.position.y = FRUSTUM_HEIGHT/2;
			guiCam.position.x = FRUSTUM_WIDTH/2;
			guiCam.zoom = 1.0f;
		}
	   

		
		else  if(world.render == 1) {
		
			guiCam.position.x = guiCam.position.x  - world.positionTouch.x + ( world.positionTouchPrev.x );
			guiCam.position.y =  guiCam.position.y - world.positionTouch.y  + (world.positionTouchPrev.y );
			
		
			guiCam.setGui();
			world.positionTouchPrev.x = world.positionTouch.x;
			world.positionTouchPrev.y = world.positionTouch.y;
			
		
			guiCam.zoom = 1.5f;
			
			
		  }
		 else if(world.render == 2){
			  	int last = world.labels.size() - 1;
				guiCam.position.y = world.labels.get(last).position.y;
				guiCam.position.x =  world.labels.get(last).position.x;
				guiCam.zoom = 1.0f;
			}
		  else if(world.render == 3){
			  	
				guiCam.position.y = guiCam.position.y;
				guiCam.position.x =  guiCam.position.x;
				guiCam.zoom = 1.0f;
			}
		  else if(world.render == 4){
			  	
			  int last = world.labels.size() - 2;
				guiCam.position.y = world.labels.get(last).position.y;
				guiCam.position.x =  world.labels.get(last).position.x;
				guiCam.zoom = 1.0f;
			}
		guiCam.setViewportAndMatrices();
		renderBackground();
		renderObjects(s);
		
	}

	private void renderBackground() {
		// TODO Auto-generated method stub
		batcher.beginBatch(Assets.background);
		batcher.drawSprite(guiCam.position.x, guiCam.position.y, FRUSTUM_WIDTH * guiCam.zoom, FRUSTUM_HEIGHT  * guiCam.zoom , Assets.backgroundRegion);
		batcher.endBatch();
		
	}

	private void renderObjects(String s) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGl();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		if(world.getLabels().size() > 0 && (world.state == World.WORLD_STATE_RUNNING || world.state == World.WORLD_STATE_LEVEL_END)){
			//Log.d("worldrenderer", "prova" + world2.resultCheck);
			batcher.beginBatch(Assets.items);
			renderLine();
			renderLabels(s);	
			batcher.endBatch();
		}
		
		gl.glDisable(GL10.GL_BLEND);
		
	}

	private void renderLine() {
		// TODO Auto-generated method stub

		int len = world.lines.size();
		if(world.state == World.WORLD_STATE_LEVEL_END){
			len = len - 1;
		}
		for(int i = 0; i < len; i++){
			
			batcher.drawLine(world.lines.get(i).pos1.x, world.lines.get(i).pos1.y, world.lines.get(i).pos2.x, world.lines.get(i).pos2.y, Assets.line);
			
	    }
		  	
	}

	private void renderLabels(String s) {
		// TODO Auto-generated method stub
		
		int len = world.labels.size();
		if(world.state == World.WORLD_STATE_LEVEL_END){
			len = len - 1;
		}
	    for(int i = 0; i < len; i++){
			Label label = world.getLabels().get(i);
			
			if(label.state == Label.LABEL_STATE_INPUT){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y  );
				
				
			}
			if (label.state == Label.LABEL_STATE_INSERT ){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelInsertRegion);
				if(label.writeEnable){
					Assets.font.drawText(batcher, s, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
					label.word = s;
				}
					
				else
					Assets.font.drawText(batcher, "", label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			if(label.state == Label.LABEL_STATE_RIGHT){
				batcher.drawSprite(label.position.x, label.position.y  , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelRightRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			if(label.state == Label.LABEL_STATE_WRONG){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelWrongRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			if(label.state == Label.LABEL_STATE_SEMI){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelSemiRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			
		}
		
	}

}



